//
//  IMSDKExtension.swift
//  ActorSDK
//
//  Created by 孙继刚 on 16/2/27.
//  Copyright © 2016年 Madordie. All rights reserved.
//

import Foundation

extension ACDialog {

    func formatToDictionary() -> [String: AnyObject] {
        var format = [String: AnyObject]()
        
        format["dialogTitle"] = dialogTitle
        format["dialogAvatar"] = dialogAvatar
        format["dialogText"] = Actor.getFormatter().formatDialogText(self)
        format["date"] = Actor.getFormatter().formatShortDate(date)
        format["unreadCount"] = "\(unreadCount)"
        format["ACPeer"] = peer
        
        var status: String!
        switch self.status.ordinal() {
        case ACMessageState.PENDING().ordinal():
            status = "PENDING"
        case ACMessageState.READ().ordinal():
            status = "READ"
        case ACMessageState.RECEIVED().ordinal():
            status = "RECEIVED"
        case ACMessageState.SENT().ordinal():
            status = "SENT"
        case ACMessageState.ERROR().ordinal():
            status = "ERROR"
        default:
            status = "other"
        }
        format["status"] = status
        
        return format
    }
}

extension ACContact {
    func formatToDictionary() -> [String: AnyObject] {
        var format = [String: AnyObject]()
        
        format["avatar"] = avatar
        format["name"] = name
        
        return format
    }
}

extension ACMessage {
    func formatToDictionary() -> [String: AnyObject] {
        var format = [String: AnyObject]()
        
        //  基础信息
        format["rid"] = "\(rid)"
        format["senderId"] = "\(Actor.getUserWithUid(senderId))"
        format["date"] = "\(Actor.getFormatter().formatDate(date))"
        format["isOut"] = isOut
        var status: String!
        switch self.messageState.ordinal() {
        case ACMessageState.PENDING().ordinal():
            status = "PENDING"
        case ACMessageState.READ().ordinal():
            status = "READ"
        case ACMessageState.RECEIVED().ordinal():
            status = "RECEIVED"
        case ACMessageState.SENT().ordinal():
            status = "SENT"
        case ACMessageState.ERROR().ordinal():
            status = "ERROR"
        default:
            status = "other"
        }
        format["messageState"] = status
        
        // content
        var type = "other"
        var content = [String: AnyObject]()
        if let text = self.content as? ACTextContent {
            type = "ACTextContent"
            
            content["text"] = text.text
            
        } else if let sticker = self.content as? ACStickerContent {
            type = "ACStickerContent"
            
            content["ACFileReference.image"] = sticker.getSticker().getFileReference512()
            content["ACFileReference.id"] = "\(sticker.getSticker().getFileReference512().getFileId())"
            content["fastThumb"] = sticker.getSticker().getFileReference256().toByteArray().toNSData()
            
        } else if let doc = self.content as? ACDocumentContent {
            type = "ACDocumentContent"
            
            //  基类处理
            if let source = doc.getSource() as? ACFileRemoteSource {
                content["ACFileReference.id"] = "\(source.getFileReference().getFileId())"
                content["ACFileReference.fileRemote"] = source.getFileReference()
            } else if let source = doc.getSource() as? ACFileLocalSource {
                content["ACFileReference.fileLocal"] = source.getFileDescriptor()
                content["ACFileReference.size"] = "\(CGFloat(source.getSize()))"
            }
            //  DOC的子类
            
            if let photo = self.content as? ACPhotoContent {
                type = "ACPhotoContent"
                
                content["W"] = "\(photo.getW())"
                content["H"] = "\(photo.getH())"
                content["fastThumb"] = photo.getFastThumb().getImage().toNSData()
            } else if let video = self.content as? ACVideoContent {
                type = "ACVideoContent"
                
                content["W"] = "\(video.getW())"
                content["H"] = "\(video.getH())"
                content["fastThumb"] = video.getFastThumb().getImage().toNSData()
            } else if let voice = self.content as? ACVoiceContent {
                type = "ACVoiceContent"
                
                content["fileSize"] = "\(Int(voice.getSource().getSize()))"
                content["duration"] = "\(Float(voice.getDuration()/1000))"
            }
            
        } else if let contact = self.content as? ACContactContent {
            type = "ACContactContent"
            var phones = [String]()
            for i in 0 ..< contact.getPhones().size() {
                phones.append(contact.getPhones().getWithInt(i) as! String)
            }
            content["phones"] = phones
            
            var emails = [String]()
            for i in 0 ..< contact.getEmails().size() {
                emails.append(contact.getEmails().getWithInt(i) as! String)
            }
            content["emails"] = emails
            
        } else if let location = self.content as? ACLocationContent {
            type = "ACLocationContent"
            
            content["latitude"] = "\(Double(location.getLatitude()))"
            content["longitude"] = "\(Double(location.getLongitude()))"
        }
        format[type] = content
        
        return format
    }
}
//
//
//extension ACPeer {
//    func formatToDictionary() -> [String: AnyObject] {
//        var format = [String: AnyObject]()
//        
//        var type = "other"
//        var content = [String: AnyObject]()
//        
//        type = "peerType"
//        switch peerType.toNSEnum() {
//        case .PRIVATE:
//            format[type] = "PRIVATE"
//        case .GROUP:
//            format[type] = "PRIVATE"
//        case .PRIVATE_ENCRYPTED:
//            format[type] = "PRIVATE_ENCRYPTED"
//        }
//        
//        format["peerId"] = "\(Int(peerId))"
//        
//        return format
//    }
//}

